package com.gzy.wbapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gzy.wbapplication.events.LoginCompleteEvent;
import com.gzy.wbapplication.manager.AuthManager;
import com.gzy.wbapplication.model.MyLog;
import com.gzy.wbapplication.model.Page;
import com.gzy.wbapplication.model.Result;
import com.gzy.wbapplication.model.para.LoginPara;
import com.gzy.wbapplication.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;

public class StartActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        EventBus.getDefault().register(this);

        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.btn_signup).setOnClickListener(this);


        if (AuthManager.isLogin(this)){
            startActivity(new Intent(this,MainActivity.class));
            EventBus.getDefault().post(new LoginCompleteEvent());
            return;
        }


//        String url = "http://wwww.baidu.com";
//        OkHttpClient okHttpClient = new OkHttpClient();
//        final Request request = new Request.Builder()
//                .url(url)
//                .get()//默认就是GET请求，可以不写
//                .build();
//        Call call = okHttpClient.newCall(request);
//        call.enqueue(new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                Log.d("", "onFailure: ");
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                Log.d("", "onResponse: " + response.body().string());
//            }
//        });



//        String str = "{\n" +
//                "                \"id\": 1,\n" +
//                "                \"userid\": 1,\n" +
//                "                \"content\": \"啊啊啊\",\n" +
//                "                \"createAt\": 1597886188790\n" +
//                "            }";
//        MyLog log = JSON.parseObject(str, MyLog.class);
//        Log.e("", "onCreate: " + log );

//        String allStr = "{\n" +
//                "    \"code\": 20000,\n" +
//                "    \"message\": \"OK\",\n" +
//                "    \"data\": {\n" +
//                "        \"data\": [\n" +
//                "            {\n" +
//                "                \"id\": 1,\n" +
//                "                \"userid\": 1,\n" +
//                "                \"content\": \"啊啊啊\",\n" +
//                "                \"createAt\": 1597886188790\n" +
//                "            }\n" +
//                "        ],\n" +
//                "        \"curPage\": 1,\n" +
//                "        \"size\": 10,\n" +
//                "        \"totalPage\": 1,\n" +
//                "        \"totalSize\": 1\n" +
//                "    }\n" +
//                "}";
//        Result<Page<MyLog>> pageResult = JSON.parseObject(allStr, new TypeReference<Result<Page<MyLog>>>() {
//        });
//        Log.e("", "onCreate: " + pageResult );



    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_login){
            startActivity(new Intent(this,LoginActivity.class));
        }else if (view.getId() == R.id.btn_signup){
           startActivity(new Intent(this,SignupActivity.class));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(LoginCompleteEvent message) {
        finish();
    }
}
